#include "draw.h"

Draw::Draw(SDL_Renderer* rR) {
    rR_ = rR;
}

Draw::~Draw() {

}

void Draw::DrawRect(Rectangle* rect) {
    SDL_Color color = rect->GetRectColor();
    SDL_SetRenderDrawColor(rR_, color.r, color.g, color.b, 255);
    SDL_RenderFillRect(rR_, rect->GetRect());

    SDL_SetRenderDrawColor(rR_, default_color_.r, default_color_.g, default_color_.b, 255);
    SDL_RenderDrawRect(rR_, rect->GetRect());

    SDL_SetRenderDrawColor(rR_, bg_color_.r, bg_color_.g, bg_color_.b, 255);
}

void Draw::DrawRect(Rectangle* rect, SDL_Color color) {
    SDL_SetRenderDrawColor(rR_, color.r, color.g, color.b, 255);
    SDL_RenderFillRect(rR_, rect->GetRect());
    SDL_SetRenderDrawColor(rR_, default_color_.r, default_color_.g, default_color_.b, 255);
    SDL_RenderDrawRect(rR_, rect->GetRect());

    SDL_SetRenderDrawColor(rR_, bg_color_.r, bg_color_.g, bg_color_.b, 255);
}

void Draw::DrawLine(SDL_Point pl, SDL_Point pr) {  
    SDL_SetRenderDrawColor(rR_, default_color_.r, default_color_.g, default_color_.b, 255);
    SDL_RenderDrawLine(rR_, pl.x, pl.y, pr.x, pr.y);
    SDL_SetRenderDrawColor(rR_, bg_color_.r, bg_color_.g, bg_color_.b, 255);
}

void Draw::DrawLine(SDL_Point pl, SDL_Point pr, Uint8 r, Uint8 g, Uint8 b) {
    SDL_SetRenderDrawColor(rR_, r, g, b, 255);
    SDL_RenderDrawLine(rR_, pl.x, pl.y, pr.x, pr.y);
    SDL_SetRenderDrawColor(rR_, bg_color_.r, bg_color_.g, bg_color_.b, 255);
}

void Draw::LinkRects(Rectangle* rect_sb, Rectangle* rect_torch) {
    SDL_Rect* rect1 = rect_sb->GetRect();
    SDL_Rect* rect2 = rect_torch->GetRect();
    SDL_Point p1 = {rect1->x + rect1->w/2, rect1->y + rect1->h/2};
    SDL_Point p2 = {rect2->x + rect2->w/2, rect2->y + rect2->h/2};

    SDL_Color color = {(Uint8)((rect_sb->GetRectColor().r + rect_torch->GetRectColor().r) / 2), 
    (Uint8)((rect_sb->GetRectColor().g + rect_torch->GetRectColor().g ) / 2),
    (Uint8)((rect_sb->GetRectColor().b + rect_torch->GetRectColor().b ) / 2)};
    this->DrawLine(p1, p2, color.r, color.g, color.b);
}
